/*
' Copyright (c) 2014 Diadrasis
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using net.diadrasis.dnn.jsImageGallery.Components;
using DotNetNuke.Common;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.FileSystem;
using System.Text.RegularExpressions;

namespace net.diadrasis.dnn.jsImageGallery
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The EditjsImageGallery class is used to manage content
    /// 
    /// Typically your edit control would be used to create new content, or edit existing content within your module.
    /// The ControlKey for this control is "Edit", and is defined in the manifest (.dnn) file.
    /// 
    /// Because the control inherits from jsImageGalleryModuleBase you have access to any custom properties
    /// defined there, as well as properties from DNN such as PortalId, ModuleId, TabId, UserId and many more.
    /// 
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class AddImageSection : jsImageGalleryModuleBase
    {

        public static string imgsec; //the image from the querystring
        public static int imgsecID; //the image id of the db
        
        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                
                if (Page.IsPostBack == false)
                {
                    imgsec = Request.QueryString["imgsecid"];
                    if (imgsec != null) {
                        imgsecID= Convert.ToInt32(imgsec);                        
                    } else {
                        imgsecID= -1;
                    }

                   
                    //in case it is an edit load the values
                    if (imgsecID > 0){
                        cImageSection editImg = cImageSectionController.GetImageSection(imgsecID);  
                        txtDesc.Text = editImg.desc;                       
                    }                  
                    
                    this.btnReturn.Attributes.Add("onclick", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("Edit", "mid=" + this.ModuleId.ToString()), this, PortalSettings, true, false));
                    //this.btnOK.Attributes.Add("onmouseup", "return " + UrlUtils.PopUpUrl(Globals.NavigateURL("Edit", "mid=" + this.ModuleId.ToString()), this, PortalSettings, true, false));
                  }

            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        protected void btnOK_Click(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            cImageSection imgsec=new cImageSection();            
            imgsec.id=imgsecID; 
            imgsec.desc=txtDesc.Text;                                
            imgsec.modID = ModuleId;
            cImageSectionController.SaveImageSection(imgsec, TabId);

            /*
            //execute javascript
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.AppendLine("<script language='javascript'>");
            sb.Append("'return'+ ");
            sb.Append("UrlUtils.PopUpUrl(Globals.NavigateURL('Edit', 'mid=' + ");            
            sb.Append(this.ModuleId.ToString());
            sb.AppendLine("), this, PortalSettings, true, false);");           
            //sb.Append(@"dnnModal.show('http://www.cnn.com',false,550,950,false)");
            //sb.Append(@"UrlUtils.PopUpUrl(Globals.NavigateURL('Edit', 'mid=' + " + this.ModuleId.ToString()+ "), this, PortalSettings, true, false);");
            sb.Append("</script>");
            System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JCall1", sb.ToString(), false);
            */

        }

       
       
    }

}